import android.content.Context
import android.util.Log
import android.widget.Toast
import com.alibaba.fastjson.JSON
import io.guangli.tutorials1v1vcall.BaseBean


/**
 * Created by Administrator on 2017/7/24.
 */
fun <S> json(context: Context, result: String, clazz: Class<S>): S? {
    val baseBean: BaseBean
    try {
        baseBean = JSON.parseObject(result, BaseBean::class.java)
        if (checkJsonBean(context, baseBean)) {
            return JSON.parseObject(result, clazz)
        }
    } catch (e: Exception) {
        Log.e(Log.getStackTraceString(e), "")
        //  T(context, "服务器繁忙，请稍后再试")
    }
    return null
}


fun <S> jsonNative(result: String, clazz: Class<S>): S? {
    try {
        return JSON.parseObject(result, clazz)
    } catch (e: Exception) {
        Log.e(Log.getStackTraceString(e),"")
        return null
    }

}

private fun checkJsonBean(context: Context, baseBean: BaseBean): Boolean {
    when (baseBean.code) {
        "200"->{
            return true
        }
        "1" -> {
            return true
        }
        "2" -> {
            return true
        }
        "3" -> {
            return true
        }
        "6" -> {
            return false
        }
        "-9999" -> {
            return true
        }
        "-203" -> {

            return false
        }
        else -> {
            Toast.makeText(context,
                baseBean.msg,Toast.LENGTH_SHORT
            ).show()
            return false
        }
    }
}
